package com.lloydfinch.composetour.ui.page

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.TextField
import androidx.compose.material.TextFieldDefaults
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import com.lloydfinch.composetour.bean.TodoItem

/**
 * Name: TodoEditLayout
 * Author: lloydfinch
 * Function: TodoEditLayout
 * Date: 2021/11/24 4:47 下午
 * Modify: lloydfinch 2021/11/24 4:47 下午
 */
@ExperimentalComposeUiApi
@Composable
fun TodoInputTextField(text: String, onTextChange: (String) -> Unit, modifier: Modifier) {
    TodoInputText(text = text, onTextChange = onTextChange, modifier = modifier)
}


@ExperimentalComposeUiApi
@Composable
fun TodoItemInput(onItemComplete: (TodoItem) -> Unit) {
    // 解构赋值
    val (text, setText) = remember { mutableStateOf("") }
    Column {
        Row(
            Modifier
                .padding(horizontal = 16.dp)
                .padding(top = 16.dp)
        ) {
            TodoInputText(
                text = text, onTextChange = setText, modifier = Modifier
                    .weight(1F)
                    .padding(end = 8.dp)
            )
            TodoEditButton(
                onClick = {
                    onItemComplete(TodoItem(text, Icons.Filled.Close))
                    setText("")
                },
                text = "Add",
                modifier = Modifier.align(Alignment.CenterVertically),
                enabled = text.isNotBlank()
            )
        }
    }
}

@ExperimentalComposeUiApi
@Composable
fun TodoInputText(text: String, onTextChange: (String) -> Unit, modifier: Modifier = Modifier, onImeAction: () -> Unit = {}) {
    // 用来控制键盘
    val keyboardController = LocalSoftwareKeyboardController.current
    TextField(
        value = text,
        onValueChange = onTextChange,
        colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.Transparent),
        keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(onDone = {
            onImeAction()
            // 隐藏键盘
            keyboardController?.hide()
        }),
        modifier = modifier
    )
}

@Composable
fun TodoEditButton(onClick: () -> Unit, text: String, modifier: Modifier = Modifier, enabled: Boolean) {

}